/**
 * created by 小卷毛, 2019-10-23 023
 * Copyright (c) 2019, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.af.community.smart.utils

import android.content.Context
import android.graphics.drawable.Drawable
import android.widget.ImageView
import androidx.annotation.DrawableRes
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.gif.GifDrawable
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.request.transition.Transition

/**
 * Glide 工具类
 */

fun Context.loadGifResource(
    @DrawableRes resourceId: Int,
    target: ImageView
) =
    Glide.with(this)
        .asGif()
        .load(resourceId)
        .into(target)

fun Context.loadGifResource(
    @DrawableRes resourceId: Int,
    listener: _CustomTarget<Drawable>.() -> Unit
) {
    val mListener = _CustomTarget<Drawable>()
    mListener.listener()

    Glide.with(this)
        .load(resourceId)
        .into(mListener)
}

fun Context.loadGifResourceWithCount(
    @DrawableRes resourceId: Int,
    target: ImageView,
    loopCount: Int
) {
    loadGifResource(resourceId) {
        onResourceReady { drawable, _ ->
            if (drawable is GifDrawable) {
                drawable.setLoopCount(loopCount)
                target.setImageDrawable(drawable)
                drawable.start()
            }
        }
    }
}

@Suppress("ClassName")
open class _CustomTarget<T> : CustomTarget<T>() {

    private var _onLoadCleared: ((Drawable?) -> Unit)? = null
    private var _onResourceReady: ((T, Transition<in T>?) -> Unit)? = null

    fun onLoadCleared(listener: (Drawable?) -> Unit) {
        _onLoadCleared = listener
    }

    fun onResourceReady(listener: (T, Transition<in T>?) -> Unit) {
        _onResourceReady = listener
    }

    override fun onLoadCleared(placeholder: Drawable?) {
        _onLoadCleared?.invoke(placeholder)
    }

    override fun onResourceReady(resource: T, transition: Transition<in T>?) {
        _onResourceReady?.invoke(resource, transition)
    }

}

open class _RequestListener<T> : RequestListener<T> {

    private var _onLoadFailed: ((GlideException?, Any, Target<T>, Boolean) -> Boolean)? = null
    private var _onResourceReady: ((T, Any, Target<T>, DataSource, Boolean) -> Boolean)? = null

    fun onLoadFailed(listener: (GlideException?, Any, Target<T>, Boolean) -> Boolean) {
        _onLoadFailed = listener
    }

    fun onResourceReady(listener: (T, Any, Target<T>, DataSource, Boolean) -> Boolean) {
        _onResourceReady = listener
    }

    override fun onLoadFailed(
        e: GlideException?,
        model: Any,
        target: Target<T>,
        isFirstResource: Boolean
    ) = _onLoadFailed?.invoke(e, model, target, isFirstResource) ?: false

    override fun onResourceReady(
        resource: T,
        model: Any,
        target: Target<T>,
        dataSource: DataSource,
        isFirstResource: Boolean
    ) = _onResourceReady?.invoke(resource, model, target, dataSource, isFirstResource) ?: false

}